//
//  RouteItemSelectCtrl.m
//  Traveling
//
//  Created by 尹桥印 on 15/6/22.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "RouteItemSelectCtrl.h"
#import "RouteSelectItemInfo.h"
#import "PlanDataTool.h"
#import "RouteItemSelectCell.h"

@interface RouteItemSelectCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *itemArray;
@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) void(^result)(ItemRouteInfo *);
@end

@implementation RouteItemSelectCtrl

-(instancetype)initWithRouteType:(RouteType)type result:(void (^)(ItemRouteInfo *routeInfo))result{
    self = [super init];
    if (self) {
        if (type==RouteTypeHotel) {
            self.url = @"journey/hotel_list.json";
            self.title = @"选择景点";
        }else{
            self.url = @"journey/scenic_list.json";
            self.title = @"选择酒店";
        }
        self.result = result;
    }
    return self;
}

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.tableView headerBeginRefreshing];
    [self.tableView registerNib:[UINib nibWithNibName:@"RouteItemSelectCell" bundle:nil] forCellReuseIdentifier:@"RouteItemSelectCell"];
    self.tableView.rowHeight = 215;
    [self.tableView setTableFooterView:[UIView new]];
    [Utils setSeparatorInsetWithTableView:self.tableView insets:UIEdgeInsetsMake(0, 8, 0, 8)];
}

-(void)headerRefresh{
    [self requestDataWithMaxId:0];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if (self.itemArray.count) {
        RouteSelectItemInfo *item = [self.itemArray lastObject];
        maxId = item.item_id-1;
    }
    [self requestDataWithMaxId:maxId];
}

-(void)requestDataWithMaxId:(NSInteger)maxId{
    [PlanDataTool getScenicAndHotelListWithMaxId:maxId url:self.url success:^(RouteSelectRes *result) {
        [Utils refreshComplete:self.tableView];
        if (result.isSucc) {
            if (maxId==0) {
                [self.itemArray removeAllObjects];
            }
            [self.itemArray addObjectsFromArray:result.datas];
            [self.tableView reloadData];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }

    } failure:^(NSError *error) {
        [Utils refreshComplete:self.tableView];
        [DialogUtils showTipsWithView:nil String:@"加载失败"];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RouteItemSelectCell *cell = [RouteItemSelectCell cellWithTableView:tableView btnClick:^(RouteSelectItemInfo *itemInfo) {
        ItemRouteInfo *info = [[ItemRouteInfo alloc] init];
        info.item_id = [NSString stringWithFormat:@"%ld",itemInfo.item_id];
        info.itemName = itemInfo.name;
        info.price = itemInfo.price;
        self.result(info);
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [cell setSelectItemInfo:self.itemArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    [Utils setSeparatorInsetWithTableViewCell:cell insets:UIEdgeInsetsMake(0, 8, 0, 8)];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}





@end
